import request from '../utils/request'

interface C2CApi {
  // 订单相关
  createOrder: (data: {
    type: 'buy' | 'sell'
    currency: string
    amount: number
    price: number
    minAmount?: number
    maxAmount?: number
    paymentMethod: string
    remark?: string
  }) => Promise<any>
  
  getOrders: (params?: {
    type?: 'buy' | 'sell'
    currency?: string
    status?: 'active' | 'completed' | 'cancelled'
    page?: number
    pageSize?: number
  }) => Promise<any>
  
  getMyOrders: (params?: {
    type?: 'buy' | 'sell'
    status?: 'active' | 'completed' | 'cancelled'
    page?: number
    pageSize?: number
  }) => Promise<any>
  
  getOrderDetail: (orderId: string) => Promise<any>
  
  acceptOrder: (orderId: string, data?: { amount?: number }) => Promise<any>
  
  cancelOrder: (orderId: string) => Promise<any>
  
  // 交易相关
  getTrades: (params?: {
    status?: 'pending' | 'paid' | 'completed' | 'cancelled'
    page?: number
    pageSize?: number
  }) => Promise<any>
  
  getTradeDetail: (tradeId: string) => Promise<any>
  
  payTrade: (tradeId: string) => Promise<any>
  
  completeTrade: (tradeId: string, remark?: string) => Promise<any>
  
  cancelTrade: (tradeId: string) => Promise<any>
}

export const c2cApi: C2CApi = {
  // 发布订单
  createOrder: (data) => {
    return request.post('/c2c/orders', data)
  },
  
  // 获取订单列表
  getOrders: (params) => {
    return request.get('/c2c/orders', { params })
  },
  
  // 获取我的订单
  getMyOrders: (params) => {
    return request.get('/c2c/orders/my', { params })
  },
  
  // 获取订单详情
  getOrderDetail: (orderId) => {
    return request.get(`/c2c/orders/${orderId}`)
  },
  
  // 接受订单
  acceptOrder: (orderId, data) => {
    return request.post(`/c2c/orders/${orderId}/accept`, data || {})
  },
  
  // 取消订单
  cancelOrder: (orderId) => {
    return request.post(`/c2c/orders/${orderId}/cancel`)
  },
  
  // 获取交易列表
  getTrades: (params) => {
    return request.get('/c2c/trades', { params })
  },
  
  // 获取交易详情
  getTradeDetail: (tradeId) => {
    return request.get(`/c2c/trades/${tradeId}`)
  },
  
  // 确认付款
  payTrade: (tradeId) => {
    return request.post(`/c2c/trades/${tradeId}/pay`)
  },
  
  // 确认收款（完成交易）
  completeTrade: (tradeId, remark) => {
    return request.post(`/c2c/trades/${tradeId}/complete`, { remark })
  },
  
  // 取消交易
  cancelTrade: (tradeId) => {
    return request.post(`/c2c/trades/${tradeId}/cancel`)
  }
}






